using System;
using System.Collections.Generic;
using System.Text;
using NHibernate.Cfg;
using NHibernate;
using NHibernate.Tool.hbm2ddl;
using System.Windows.Forms;

namespace tKitchen
{
    public class DataAccess
    {
        ISessionFactory factory;
        Configuration cfg;

        public ISessionFactory Factory
        {
            get { return factory; }
            set { factory = value; }
        }

        public DataAccess()
        {
            cfg = new Configuration().Configure("hibernateCE.hbm.xml");
            //ResetDatabase();
            factory = cfg.BuildSessionFactory();
        }
        public DataAccess(string xmlIn)
        {
            cfg = new Configuration().Configure(xmlIn);
            ResetDatabase();
            factory = cfg.BuildSessionFactory();
        }

        public void ResetDatabase()
        {
            //drops and creates the database tables 
            new SchemaExport(cfg).Create(true, true);
        }


        #region Get Functions
        public object GetObject(Type type, int id)
        {
            object temp = null;
            try
            {
                ISession session = factory.OpenSession();
                temp = session.Get(type, id);
                session.Close();

            }
            catch (Exception e)
            {
                DisplayErrorMessage(e);
            }
            return temp;
        }

        /// <summary>
        /// Gets all MealPlanPO objects from the database
        /// </summary>
        /// <returns></returns>
        public List<MealPlanPO> GetMealPlanList()
        {
            List<MealPlanPO> list = new List<MealPlanPO>();
            try
            {
                ISession session = factory.OpenSession();
                list = (List<MealPlanPO>)session.CreateCriteria(typeof(MealPlanPO)).List<MealPlanPO>();
                session.Close();
            }
            catch (Exception e)
            {
                DisplayErrorMessage(e);
            }
            return list;
        }
        public List<UnitPO> GetUnitList()
        {
            List<UnitPO> list = new List<UnitPO>();
            try
            {
                ISession session = factory.OpenSession();
                list = (List<UnitPO>)session.CreateCriteria(typeof(UnitPO)).List<UnitPO>();
                session.Close();
            }
            catch (Exception e)
            {
                DisplayErrorMessage(e);
            }
            return list;

        }
        public List<RecipeCategoryPO> GetRecipeCategoryList()
        {
            List<RecipeCategoryPO> list = new List<RecipeCategoryPO>();
            try
            {
                ISession session = factory.OpenSession();
                list = (List<RecipeCategoryPO>)session.CreateCriteria(typeof(RecipeCategoryPO)).List<RecipeCategoryPO>();
                session.Close();
            }
            catch (Exception e)
            {
                DisplayErrorMessage(e);
            }
            return list;

        }
        public List<IngredientPO> GetIngredientList()
        {
            List<IngredientPO> list = new List<IngredientPO>(); ;
            try
            {
                ISession session = factory.OpenSession();
                list = (List<IngredientPO>)session.CreateCriteria(typeof(IngredientPO)).List<IngredientPO>();
                session.Close();
            }
            catch (Exception e)
            {
                DisplayErrorMessage(e);
            }
            return list;
        }
        public List<RecipePO> GetRecipeList()
        {
            List<RecipePO> list = new List<RecipePO>();
            try
            {
                ISession session = factory.OpenSession();
                list = (List<RecipePO>)session.CreateCriteria(typeof(RecipePO)).List<RecipePO>();
                session.Close();
            }
            catch (Exception e)
            {
                DisplayErrorMessage(e);
            }
            return list;
        }
        #endregion

        public int Save(object objIn)
        {
            int id = -1;
            try
            {
                ISession session = factory.OpenSession();
                id = (int)session.Save(objIn);
                session.Flush();
                session.Close();
            }
            catch (Exception e)
            {
                DisplayErrorMessage(e);
            }
            return id;

        }

        public void Update(object objIn)
        {
            try
            {
                ISession session = factory.OpenSession();
                session.Update(objIn);
                session.Flush();
                session.Close();
            }
            catch (Exception e)
            {
                DisplayErrorMessage(e);
            }
        }

        public void Delete(object objIn)
        {
            try
            {
                ISession session = factory.OpenSession();
                session.Delete(objIn);
                session.Flush();
                session.Close();
            }
            catch (Exception e)
            {
                DisplayErrorMessage(e);
            }
        }

        private void DisplayErrorMessage(Exception eIn)
        {
            MessageBox.Show("An Error has occured!\n" + eIn, "Error!");
        }

    }
}
